package handlers

import (
	"ginmt/handlers/requests"
	"ginmt/handlers/response"
	"ginmt/logic"
	"ginmt/pkg"

	"github.com/gin-gonic/gin"
	"github.com/silenceper/wechat/v2/officialaccount/js"
)

func WechatJsConfig(appCtx *pkg.AppCtx) func(c *gin.Context) {
	return func(c *gin.Context) {
		var request requests.WeChatJsConfig
		err := c.ShouldBindBodyWithJSON(&request)
		if err != nil {
			response.Failed(c, response.FailedCode, err.Error())
			return
		}
		wechatLogic := logic.NewWebChatLogic(appCtx, c)

		config, err := wechatLogic.GetJsConfig(request.Uri)
		if err != nil {
			response.Failed(c, response.FailedCode, err.Error())
			return
		}

		type jsconfig struct {
			*js.Config
			JsApiList []string `json:"jsApiList"`
		}

		var reConfig jsconfig

		reConfig.Config = config
		reConfig.JsApiList = []string{}
		response.Success(c, response.SuccessCode, reConfig)
	}
}
